#include"stc.h"

int m[1000] = {0};
void init()
{
	int i = 0;
	while(i<=999)
	{
		if(0<=i&&i<=9) 
			m[i] = -1;
		else
		{

			int d1 = i/100;
			int d2 = i/10%10;
			int d3 = i%10;
			if(d1!=d2&&d2!=d3&&d3!=d1) 
				m[i] = 0;
			else
				m[i] = -1;
		}
		i = i + 1;
	}
}

void main()
{
	int average = 0;
	int i = 0;
	while(i<1000)
	{
		bool stop = false;
		int r1 = 0;
		int r2 = 0;
		int r3 = 0;
		while(!stop)
		{
			 r1 = RandNum()%10;
			 r2 = RandNum()%10;
			 r3 = RandNum()%10;
			if(r1==r2||r2==r3||r3==r1) 
				stop = false;
			else
				stop = true;
		}
		PutStr("------------");
		PutInt(i+1);
		PutStr("------------");
		PutStr("\nthree number ");
		PutInt(r1);
		PutStr(" ");
		PutInt(r2);
		PutStr(" ");
		PutInt(r3);
		PutStr("\n");
		init();
		int max = 0;
		int j = 0;
		int count = 0;
		while(j<1000)
		{

			if(m[j]==max)
			{
				int strike = 0;
				int ball = 0;
				PutStr("try ");
				PutInt(count + 1);
				PutStr(".\n");
				int d1 = j/100;
				int d2 = j/10%10;
				int d3 = j%10;
				PutInt(d1);
				PutStr(" ");
				PutInt(d2);
				PutStr(" ");
				PutInt(d3);
				PutStr("\n");
				strike = (d1==r1)+(d2==r2)+(d3==r3);
				ball = (d1==r2)+(d1==r3)+(d2==r1)+(d2==r3)+(d3==r1)+(d3==r2);
				PutStr("strike : ");
				PutInt(strike);
				PutStr("            ");
				PutStr("ball : ");
				PutInt(ball);
				PutStr("\n\n");
				count = count + 1;
				if(strike == 3)
				{
					PutStr("success!!!");
					PutStr("            Count: ");
					PutInt(count);
					PutStr("\n\n");
					average = average + count;
					break;
				}
				int k = j + 1;
				while(k<1000)
				{
					int a1 = k/100;
					int a2 = k/10%10;
					int a3 = k%10;
					int strike1 = (a1==d1)+(a2==d2)+(a3==d3);
					int ball1 = (a1==d2)+(a1==d3)+(a2==d1)+(a2==d3)+(a3==d1)+(a3==d2);
					if(m[k]==max&&strike1==strike&&ball1==ball)
					{
						m[k] = max + 1;
					}
					k = k + 1;
				}
					max = max + 1;	
			}
		
			j = j + 1;
		}
		i = i + 1;
	}
	PutStr("-----------end------------");
	PutStr("\n");
	PutStr("\nAverage : ");
	average = average/1000;
	PutInt(average);
	PutStr("\n");
	GetChar();
}